import java.util.HashMap;
import java.util.Map;

class Solution {
    public int singleNumber(int[] nums) {
        //以键为数，以值为量
        Map <Integer, Integer> hash = new HashMap<>();

        for (Integer num : nums) {
            //不存在则为空
            Integer count = hash.get(num);
            count = count == null ? 1 : ++count;
            hash.put(num, count);
        }

        //遍历键
        for (Integer num : hash.keySet()) {
            Integer count = hash.get(num);
            if (count == 1) {
                return num;
            }
        }

        return -1;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();

        // 测试用例1：存在唯一元素
        int[] nums1 = {2, 2, 1};
        int result1 = solution.singleNumber(nums1);
        System.out.println("测试用例1: 唯一元素是 " + result1); // 输出1

        // 测试用例2：唯一元素在中间
        int[] nums2 = {4, 1, 2, 1, 2};
        int result2 = solution.singleNumber(nums2);
        System.out.println("测试用例2: 唯一元素是 " + result2); // 输出4

        // 测试用例3：负数和重复元素
        int[] nums3 = {-1, -2, -1};
        int result3 = solution.singleNumber(nums3);
        System.out.println("测试用例3: 唯一元素是 " + result3); // 输出-2

        // 测试用例4：空数组（根据题目假设不会出现）
        int[] nums4 = {};
        int result4 = solution.singleNumber(nums4);
        System.out.println("测试用例4: 结果是 " + result4); // 输出-1
    }
}